const ObjId = require('../common/ObjId');

class PostComment {
    constructor(obj) {
        this.requestTime = obj.time;
        this.postId = obj.body.postId;
        this.time = obj.body.time;
        this.content = obj.body.content;
    }

    check() {
        let { time, content, requestTime } = this;
        // 验证 id
        let post = new ObjId(this, 'postId');
        let flag = post.check();
        if (!flag.success) {
            return flag;
        }
        // 验证内容
        if (!content) {
            return { success: false, err: '内容为空' };
        }
        // 判断 内容 去除空白字符之后是否为空内容
        let format_circleContent = content.replace(/\s+/g, '');
        if (!format_circleContent) {
            return { success: false, err: '评论内容不允许为空' };
        }
        // 时间判空
        if (!time) {
            return { success: false, err: '时间为空' };
        }
        // 验证时间的正确性
        let publishTimeObj;
        try {
            publishTimeObj = new Date(time);
        } catch (err) {
            return { success: false, err: '时间格式错误' };
        }
        // 时间节点验证
        if (publishTimeObj > requestTime) {
            return { success: false, err: '时间节点错误' };
        }
        // 超时验证
        if (new Date(publishTimeObj.getTime() + 5 * 60 * 1000) < requestTime) {
            return { success: false, err: '超时5分钟' };
        }
        return { success: true };
    }
}

module.exports = PostComment;